<template >
  <div>
    <el-form :inline="true" :model="from" class="demo-form-inline">
      <el-form-item label="房号状态">
        <el-select v-model="from.roomState" placeholder="请选择" clearable @change="Getroom">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="房型状态">
        <el-select v-model="from.houseState" placeholder="请选择" clearable @change="Getroom">
          <el-option v-for="item in houselist" :key="item.id" :label="item.houseName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="room">房号添加</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="multipleTable" :data="roomlist" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="houseName" label="房型名称" width="120">
      </el-table-column>
      <el-table-column prop="roomNumber" label="房号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="roomSort" label="排序" show-overflow-tooltip></el-table-column>
      <el-table-column prop="roomRemark" label="备注" show-overflow-tooltip></el-table-column>
      <el-table-column prop="roomState" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.roomState == 1">上架</span>
          <span v-if="scope.row.roomState == 2">下架</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="Getstate(scope.row.id)"
            v-if="scope.row.roomState == 1">下架</el-button>
          <el-button size="mini" type="danger" @click="Getstate(scope.row.id)"
            v-if="scope.row.roomState == 2">上架</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="房号添加" :visible.sync="dialogFormVisible">
      <add-room ref="addRoom" v-if="dialogFormVisible"></add-room>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="Room">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="房号修改" :visible.sync="dialogFormVisible1">
      <upd-room :Rid="roomid" ref="updRoom" v-if="dialogFormVisible1"></upd-room>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="RoomUpd">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[2, 3, 6, 9]"
      :page-size="from.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="count">
    </el-pagination>
  </div>
</template>
<script>
import { getroom, gethouse, getstate, deleteroom } from "@/api/room";
import AddRoom from "./AddRoom.vue";
import UpdRoom from "./UpdRoom.vue";
export default {
  components: { AddRoom, UpdRoom },
  data() {
    return {
      roomlist: [],
      houselist: [],
      from: {
        pageIndex: 1,
        pageSize: 3,
        roomState: "",
        houseState: "",
      },
      options: [
        {
          value: "1",
          label: "上架",
        },
        {
          value: "2",
          label: "下架",
        },
      ],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      roomid: 0,
      count: 0,
    };
  },
  methods: {
    Getroom() {
      getroom(this.from).then((res) => {
        this.roomlist = res.data;
        this.count = res.count;
      });
    },
    handleSizeChange(val) {
      this.from.pageSize = val;
      this.Getroom();
    },
    handleCurrentChange(val) {
      this.from.pageIndex = val;
      this.Getroom();
    },
    //复选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    gethouse() {
      gethouse().then((res) => {
        this.houselist = res.data;
      });
    },
    Getstate(id) {
      debugger;
      getstate(id).then((res) => {
        if (res.data) {
          this.$message.success("修改成功");
          this.Getroom();
        } else {
          this.$message.success("修改失败");
        }
      });
    },
    room() {
      this.dialogFormVisible = true;
    },
    //添加
    Room() {
      this.$refs.addRoom.AddRoom();
    },
    //修改
    RoomUpd() {
      this.$refs.updRoom.UpdRoom();
    },
    handleEdit(id) {
      this.roomid = id;
      this.dialogFormVisible1 = true;
    },
    //删除
    handleDelete(id) {
      this.$confirm('确定要删除吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteroom(id).then(res => {
          if (res.data) {
            this.$message.success("删除成功");
            this.Getroom();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });




    }
  },
  created() {
    this.Getroom();
    this.gethouse();
  },
};
</script>
<style ></style>